package com.tianyang.yukuaifu.business.trade.presenter;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.tianyang.yukuaifu.business.trade.bean.AgentWalletOutput;
import com.tianyang.yukuaifu.business.trade.bean.EarningsOutput;
import com.tianyang.yukuaifu.business.trade.bean.EarningsTypeOutput;
import com.tianyang.yukuaifu.business.trade.contract.EarningsContract;
import com.tianyang.yukuaifu.business.trade.model.TradeModel;
import com.tianyang.yukuaifu.common.interfaces.CommonConfig;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：收益
 * Created by xyj on 2024/3/10
 */
public class EarningsPresenter implements EarningsContract.Presenter {

    private EarningsContract.View mView;

    private CompositeSubscription mSubscription;

    public EarningsPresenter(EarningsContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void getSumUserWallet() {
        mView.showLoadingDialog(false);
        Subscription subscription = TradeModel.getInstance().getSumUserWallet()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<EarningsTypeOutput>>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showLoadMore(CommonConfig.LOADMOREFAIL);
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(List<EarningsTypeOutput> outputs) {
                        mView.showEarningsTypeView(outputs);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void getEarningsList(int pageNumber, int serviceType) {
        Subscription subscription = TradeModel.getInstance().getUserWallet(pageNumber, serviceType)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<JSONObject>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showEarningsListView(null, pageNumber == CommonConfig.PAGE_NUM);
                        mView.showLoadMore(CommonConfig.LOADMOREFAIL);
                    }

                    @Override
                    public void onNext(JSONObject outputs) {
                        List<EarningsOutput> earningsOutputs = null;
                        int totalPage = 0;
                        if (outputs != null) {
                            totalPage = outputs.getIntValue("totalPage");
                            earningsOutputs = outputs.getObject("list", new TypeReference<List<EarningsOutput>>(){});
                            Object obj = outputs.get("obj");
                            if (!(obj instanceof JSONObject)) {
                                JSONObject jsonObject = new JSONObject();
                                jsonObject.put("totalAmount", obj);
                                mView.showEarningsWallet(jsonObject);
                            } else {
                                mView.showEarningsWallet(outputs.getJSONObject("obj"));
                            }
                        }
                        mView.showEarningsListView(earningsOutputs, pageNumber == CommonConfig.PAGE_NUM);
                        if (pageNumber < totalPage) {
                            mView.showLoadMore(CommonConfig.LOADMORECOMPLETE);
                        } else {
                            mView.showLoadMore(CommonConfig.LOADMOREEND);
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void getAgentWallet() {
        mView.showLoadingDialog(false);
        Subscription subscription = TradeModel.getInstance().getAgentWallet()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<AgentWalletOutput>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(AgentWalletOutput output) {
                        mView.showAgentWallet(output);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
